package com.tjnmt.togo.utils;

import android.app.Activity;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * sp操作工具类。
 */
public class SharedPreferencesUtil {
    private static final SharedPreferences sharedPreferences;
    private static final SharedPreferences.Editor editor;

    static {
        sharedPreferences = ApplicationUtils.getContext().getSharedPreferences("sp", Activity.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static void putData(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            editor.putString(entry.getKey(), entry.getValue());
        }
        editor.commit();
    }

    public static void putData(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public static Map<String, String> getData(Map<String, String> map) {
        Map<String, String> data = new HashMap<String, String>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = sharedPreferences.getString(key, entry.getValue());
            data.put(key, value);
        }
        return data;
    }

    public static String getData(String key, String defValue) {
        String value = sharedPreferences.getString(key, defValue);
        return value;
    }

    public static void clearData() {
        editor.clear();
        editor.commit();
    }
}
